/**
 * @file store 入口
 * 使用vuex一定要注意prop和state的区别，既然使用了vuex，
 * 那么所有的props数据源头都走vuex，否则就vuex失去了意义。
 */
import Vuex from 'vuex'
import Vue from 'vue'
import actions from './actions'
import mutations from './mutations'
import MemoryFs from '../memory-fs/memoryFs'

function sortFileArray (children) {
  children && children.sort(function (a, b) {
    const a1 = a.children ? 1 : 0
    const b1 = b.children ? 1 : 0
    return b1 - a1 || a.name > b.name
  })
}

function traverseSortNode (root) {
  if (!root.children) {
    return
  }
  sortFileArray(root.children)
  for (let i in root.children) {
    traverseSortNode(root.children[i])
  }
}

Vue.use(Vuex)

const state = {
  menuState: {x: 0, y: 0, show: false},
  menuList: [],
  selectedFile: '',
  terminalInfo: {
    type: 'common', // success/error/common
    message: '欢迎使用hap-ide，编译之前请先保存...'
  },
  memoryFs: new MemoryFs(),
  fileObject: {},
  toasts: [],
  bundleSrc: ''
}

const getters = {
  catelog (state) {
    const root = state.memoryFs.root
    traverseSortNode(root)
    return root
  }
}

export default new Vuex.Store({
  state,
  actions,
  mutations,
  getters
})
